/**
 * 维护用户登录状态、身份信息
 */
import { Service } from '@gnlab/utils'
import { type Services } from '@/services'
import * as apps from './apps'
import {
  loginByDetectedToken,
  info$,
  account$,
  accountId,
  isAdmin$,
  inServiceMode$,
  unauthorized$,
} from './info'
import * as internal from './internal'
import * as observing from './observing'

export * from './definition'

// 执行业务行为时使用的 accountId 列表
// 观察者模式下是被观察的账号列表；普通模式下是登录账号 ID
export const bizAccountIds = Promise.all([accountId, observing.accountIds]).then(
  ([realAccountId, observingAccountIds]) => observingAccountIds ?? [realAccountId],
)

export class Auth extends Service<Services> {
  registered() {
    loginByDetectedToken()
  }

  readonly unauthorized$ = unauthorized$
  readonly info$ = info$
  readonly account$ = account$
  readonly accountId = accountId
  readonly isAdmin$ = isAdmin$
  readonly inServiceMode$ = inServiceMode$
  readonly bizAccountIds = bizAccountIds

  readonly apps = apps
  readonly internal = internal
  readonly observing = observing
}
